package com.share.cloudStorage.users.service;

import com.aliyun.dysmsapi20170525.models.SendSmsRequest;
import org.springframework.stereotype.Service;

@Service
public class SmsService {
    private final com.aliyun.dysmsapi20170525.Client client;

    public SmsService() throws Exception {
        this.client = createClient();
    }
    private static com.aliyun.dysmsapi20170525.Client createClient() throws Exception {
        com.aliyun.teaopenapi.models.Config config = new com.aliyun.teaopenapi.models.Config()
                .setAccessKeyId("LTAI5tCs1XsYryr4TKWq6TTn")
                .setAccessKeySecret("yxl6FqF7QIFzX3ILReo1WgjzbrjstG");
        config.endpoint = "dysmsapi.aliyuncs.com";
        return new com.aliyun.dysmsapi20170525.Client(config);
    }
    public boolean send(String num, String code) {
        try {
            SendSmsRequest sendSmsRequest = new SendSmsRequest()
                    .setPhoneNumbers(num)
                    .setSignName("企业云盘")
                    .setTemplateCode("SMS_475950807")
                    .setTemplateParam(String.format("{\"code\":\"%s\"}", code));
            // 发送短信并获取响应
            com.aliyun.dysmsapi20170525.models.SendSmsResponse response = client.sendSms(sendSmsRequest);
            // 检查发送结果
            if ("OK".equals(response.getBody().getCode())) {
                return true;
            } else {
                // 日志错误信息
                System.err.println("Failed to send SMS: " + response.getBody().getMessage());
                return false;
            }
        } catch (Exception e) {
            // 记录异常
            e.printStackTrace();
            return false;
        }
    }
}
